﻿Public Class FTInputSiswa

    'member variables
    Private _cSiswa As CSiswa
    Private _pekerjaanAyah As tbl_pekerjaan
    Private _pekerjaanIbu As tbl_pekerjaan
    Private _pekerjaanWali As tbl_pekerjaan
    Private _cSemester As CSemester
    Private _semesterAktif As DataTable

    'forms
    Private WithEvents _flPekerjaan As FLPekerjaan
    Private WithEvents _flKelas As FLKelas

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        'ContextMenuStripAgama.SetBounds(tbAgama.Location.X, tbAgama.Location.Y, tbAgama.Width, tbAgama.Height * 5)
    End Sub

#Region "SettinganAwal"
    '-----------------------------------------------------------------------------------------------
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)

        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub


    Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim p As New Pen(Color.FromArgb(213, 251, 185), 2)
        'set text box border
        PaintTextBox(Me, e.Graphics, p)

        p.Dispose()
    End Sub

    Private Sub PaintTextBox(ByVal cont As Control, ByVal g As Graphics, ByVal p As Pen)
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is TextBox Then
                g.DrawRectangle(p, New Rectangle(ctl.Location + New Size(1, 1), ctl.Size - New Size(2, 2)))
            ElseIf TypeOf ctl Is Panel Then
                PaintTextBox(ctl, g, p)
            ElseIf TypeOf ctl Is GroupBox Then
                PaintTextBox(ctl, g, p)
            End If
        Next
    End Sub

    Private Sub SetRegionTextBox(ByVal cont As Control)
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is TextBox Then
                If ctl.Region Is Nothing Then
                    ctl.Region = New Region(New Rectangle(2, 2, ctl.Width - 4, ctl.Height - 4))
                Else
                    ctl.Region = Nothing
                End If
            ElseIf TypeOf ctl Is Panel Then
                SetRegionTextBox(ctl)
            ElseIf TypeOf ctl Is GroupBox Then
                SetRegionTextBox(ctl)
            End If
        Next
    End Sub

    Private Sub btnMinimize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        WindowState = FormWindowState.Minimized
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub
    '-----------------------------------------------------------------------------------------------
#End Region

    Private Sub FirstLater_Validated(sender As System.Object, e As System.EventArgs) Handles tbNama.Validated, tbTempatLahir.Validated, tbAlamat.Validated, tbNamaAyah.Validated, tbNamaIbu.Validated, tbNamaWali.Validated, tbAlamatAyah.Validated, tbAlamatIbu.Validated, tbAlamatWali.Validated
        'konversi teks huruf depan besar
        sender.Text = StrConv(sender.Text, VbStrConv.ProperCase)
    End Sub

    Private Sub KkTextBox5_Enter(sender As System.Object, e As System.EventArgs) Handles tbAgama.Enter, tbAgama.Click
        ContextMenuStripAgama.Show(tbAgama, 0, 0)
    End Sub

    Private Sub tbJenisKelamin_Enter(sender As Object, e As System.EventArgs) Handles tbJenisKelamin.Enter, tbJenisKelamin.Click
        ContextMenuStripJenisKelamin.Show(tbJenisKelamin, 0, 0)
    End Sub

    Private Sub ContextMenuStripAgama_ItemClicked(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ContextMenuStripAgama.ItemClicked
        tbAgama.Text = e.ClickedItem.Text
        tbAgamaValue.Text = e.ClickedItem.Tag
    End Sub

    Private Sub ContextMenuStripJenisKelamin_ItemClicked(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ContextMenuStripJenisKelamin.ItemClicked
        tbJenisKelamin.Text = e.ClickedItem.Text
        tbJenisKelaminValue.Text = e.ClickedItem.Tag
    End Sub

    Private Sub btnKeluar_Click(sender As System.Object, e As System.EventArgs) Handles btnKeluar.Click
        KeluarToolStripMenuItem.PerformClick()
    End Sub


    Private Sub SimpanToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SimpanToolStripMenuItem.Click
        'simpan siswa
        _cSiswa = New CSiswa
        Dim siswaBaru As New tbl_siswa
        With siswaBaru
            .NISN = tbNomorInduk.Text.Trim
            .Nama = tbNama.Text.Trim
            .TempatLahir = tbTempatLahir.Text.Trim
            .TanggalLahir = DateSerial(tbTahun.Text.Trim, tbBulan.Text.Trim, tbTanggal.Text.Trim)
            .JenisKelamin = tbJenisKelaminValue.Text.Trim
            .Alamat = tbAlamat.Text.Trim
            .AnakKe = IIf(tbAnakKe.Text.Trim = "", 0, tbAnakKe.Text.Trim)
            .JumlahSaudara = IIf(tbJumlahsaurada.Text.Trim = "", 0, tbJumlahsaurada.Text.Trim)
            .NamaAyah = tbNamaAyah.Text.Trim
            .NamaIbu = tbNamaIbu.Text.Trim
            .AlamatAyah = tbAlamatAyah.Text.Trim
            .AlamatIbu = tbAlamatIbu.Text.Trim
            .TelpAyah = tbTelpAyah.Text.Trim
            .TelpIbu = tbTelpIbu.Text.Trim
            .IdKerjaAyah = IIf(tbPekerjaanAyah.Text.Trim <> "", _pekerjaanAyah.id, Nothing)
            .IdKerjaIbu = IIf(tbPekerjaanIbu.Text.Trim <> "", _pekerjaanIbu.id, Nothing)
            .NamaWali = tbNamaWali.Text.Trim
            .TelpWali = tbTelpWali.Text.Trim
            .AlamatWali = tbAlamatWali.Text.Trim
            .foto = ""
            .Tingkatan = tbKelasValue.Text.Trim
        End With

        If tbKelasValue.Text.Trim = "B" Then
            _cSiswa.simpan(siswaBaru)
        Else
            Dim aKelas As New tbl_kelas
            With aKelas
                .id = tbRombelValue.Text.Trim
                .nama = tbRombel.Text.Trim
            End With
            Dim aSemester As New tbl_semester
            With aSemester
                .id = _semesterAktif(0).Item("id")
                .nama = _semesterAktif(0).Item("nama")
            End With
            _cSiswa.simpan(siswaBaru, aKelas, aSemester)
        End If

        ResetToolStripMenuItem.PerformClick()
        tbNama.Focus()
    End Sub

    Private Sub KeluarToolStripMenuItem_Click_1(sender As System.Object, e As System.EventArgs) Handles KeluarToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub btnSimpan_Click(sender As System.Object, e As System.EventArgs) Handles btnSimpan.Click
        SimpanToolStripMenuItem.PerformClick()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'reset text field
        ResetToolStripMenuItem.PerformClick()
    End Sub

    Private Sub ResetForm()
        For Each ctl As Control In Me.Panel2.Controls
            If TypeOf ctl Is TextBox Then
                CType(ctl, TextBox).Clear()
            End If
        Next
    End Sub

    Private Sub ResetToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ResetToolStripMenuItem.Click
        ResetForm()
    End Sub

    Private Sub Tbl_PekerjaanAyah_Click(sender As Object, e As System.EventArgs) Handles tbPekerjaanAyah.Click, tbPekerjaanAyah.Enter, tbPekerjaanIbu.Click, tbPekerjaanIbu.Enter, tbPekerjaanWali.Enter, tbPekerjaanWali.Click
        'form cover
        Dim fCov As New FCover
        fCov.Show()

        'tampilkan form daftar pekerjaan
        _flPekerjaan = New FLPekerjaan
        _flPekerjaan.ShowDialog()

        fCov.Close()
    End Sub

    Private Sub _flPekerjaan_DataTerpilih() Handles _flPekerjaan.DataTerpilih
        If TypeOf ActiveControl Is TextBox Then
            Dim aTb As TextBox = CType(ActiveControl, TextBox)

            aTb.Text = _flPekerjaan.getSelectedPekerjaan.Nama

            If aTb Is tbPekerjaanAyah Then
                _pekerjaanAyah = _flPekerjaan.getSelectedPekerjaan
            ElseIf aTb Is tbPekerjaanIbu Then
                _pekerjaanIbu = _flPekerjaan.getSelectedPekerjaan
            ElseIf aTb Is tbPekerjaanWali Then
                _pekerjaanWali = _flPekerjaan.getSelectedPekerjaan
            End If

        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub tbKelas_Enter(sender As System.Object, e As System.EventArgs) Handles tbKelas.Enter, tbKelas.Click
        'tampilkan list kelas
        ContextMenuStripTingkatan.Show(tbKelas, 0, 0)
    End Sub

    Private Sub ContextMenuStripTingkatan_ItemClicked(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ContextMenuStripTingkatan.ItemClicked
        tbKelasValue.Text = e.ClickedItem.Tag
        tbKelas.Text = e.ClickedItem.Text

        'set rombel
        If tbKelasValue.Text.Trim <> "" And tbKelasValue.Text.Trim <> "B" Then
            tbRombel.Visible = True
            lblRombel.Visible = True
        Else
            tbRombel.Visible = False
            lblRombel.Visible = False
        End If
    End Sub

    Private Sub FTInputSiswa_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'set text box border
        SetRegionTextBox(Me)

        'set semester aktif
        _cSemester = New CSemester()
        _semesterAktif = _cSemester.getSemesterAktif

        'set auto complete tempat lahir
        tbTempatLahir.AutoCompleteMode = AutoCompleteMode.Append
        tbTempatLahir.AutoCompleteSource = AutoCompleteSource.CustomSource
        Dim tmptList As New AutoCompleteStringCollection
        _cSiswa = New CSiswa
        For Each aRow As DataRow In _cSiswa.getTempatLahirSiswa.Rows
            tmptList.Add(aRow.Item(0))
        Next
        tbTempatLahir.AutoCompleteCustomSource = tmptList

        tbRombel.Visible = False
    End Sub

    Private Sub tbRombel_Enter(sender As Object, e As System.EventArgs) Handles tbRombel.Enter, tbRombel.Click
        'form cover
        Dim fCov As New FCover
        fCov.Show()

        _flKelas = New FLKelas
        _flKelas.ShowDialog()

        fCov.Close()
    End Sub

    Private Sub _flKelas_DataTerpilih() Handles _flKelas.DataTerpilih
        'set data kelas/rombel terpilih
        tbRombel.Text = _flKelas.SelectedNama
        tbRombelValue.Text = _flKelas.SelectedID
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        'form cover
        Dim fCov As New FCover
        fCov.Show()

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
        End If

        fCov.Close()
    End Sub
End Class